/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.mhm.service.impl;

import me.zhengjie.modules.cspimp.mhm.domain.ReservoirAreaManage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.mhm.repository.ReservoirAreaManageRepository;
import me.zhengjie.modules.cspimp.mhm.service.ReservoirAreaManageService;
import me.zhengjie.modules.cspimp.mhm.service.dto.ReservoirAreaManageDto;
import me.zhengjie.modules.cspimp.mhm.service.dto.ReservoirAreaManageQueryCriteria;
import me.zhengjie.modules.cspimp.mhm.service.mapstruct.ReservoirAreaManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ReservoirAreaManageServiceImpl implements ReservoirAreaManageService {

    private final ReservoirAreaManageRepository reservoirAreaManageRepository;
    private final ReservoirAreaManageMapper reservoirAreaManageMapper;

    @Override
    public Map<String,Object> queryAll(ReservoirAreaManageQueryCriteria criteria, Pageable pageable){
        Page<ReservoirAreaManage> page = reservoirAreaManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reservoirAreaManageMapper::toDto));
    }

    @Override
    public List<ReservoirAreaManageDto> queryAll(ReservoirAreaManageQueryCriteria criteria){
        return reservoirAreaManageMapper.toDto(reservoirAreaManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public ReservoirAreaManageDto findById(Long id) {
        ReservoirAreaManage reservoirAreaManage = reservoirAreaManageRepository.findById(id).orElseGet(ReservoirAreaManage::new);
        ValidationUtil.isNull(reservoirAreaManage.getId(),"ReservoirAreaManage","id",id);
        return reservoirAreaManageMapper.toDto(reservoirAreaManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReservoirAreaManageDto create(ReservoirAreaManage resources) {
        return reservoirAreaManageMapper.toDto(reservoirAreaManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReservoirAreaManage resources) {
        ReservoirAreaManage reservoirAreaManage = reservoirAreaManageRepository.findById(resources.getId()).orElseGet(ReservoirAreaManage::new);
        ValidationUtil.isNull( reservoirAreaManage.getId(),"ReservoirAreaManage","id",resources.getId());
        reservoirAreaManage.copy(resources);
        reservoirAreaManageRepository.save(reservoirAreaManage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            reservoirAreaManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ReservoirAreaManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReservoirAreaManageDto reservoirAreaManage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", reservoirAreaManage.getCreateTime());
            map.put("更新时间", reservoirAreaManage.getUpdateTime());
            map.put("创建者", reservoirAreaManage.getCreateBy());
            map.put("更新者", reservoirAreaManage.getUpdateBy());
            map.put("库区名称", reservoirAreaManage.getReservoirAreaName());
            map.put("库房数量", reservoirAreaManage.getWarehouseNumber());
            map.put("库房序号", reservoirAreaManage.getWarehouseSerialNumber());
            map.put("库房名称", reservoirAreaManage.getWarehouseName());
            map.put("库房形式", reservoirAreaManage.getWarehouseModus());
            map.put("库房结构", reservoirAreaManage.getWarehouseStructure());
            map.put("储存类型", reservoirAreaManage.getStorageType());
            map.put("储存量", reservoirAreaManage.getStorageNumber());
            map.put("审核周期", reservoirAreaManage.getAuditCycle());
            map.put("火灾等级", reservoirAreaManage.getFireLevel());
            map.put("上次审核时间", reservoirAreaManage.getLastAuditTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}